package life.upmenu.View;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import life.upmenu.R;

/**
 * Created by Zhou HongChun on 2017/7/7.
 *
 */

public class MyAdapter extends RecyclerView.Adapter {
    private final Context mContext;
    private final int[] mImageviews;
    int nexPosition = 0;
    int k;

    public MyAdapter(Context context, int[] imageviews) {
        mContext = context;
        mImageviews = imageviews;
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_view, parent, false);
        return new MyViewHolder(view);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        Log.e("answer","mImageviews.length % 4: " + mImageviews.length % 4+"      nexPosition:  " + nexPosition + "      " + "mImageviews.length - mImageviews.length % 4:  " + (mImageviews.length - mImageviews.length % 4) + "");

        if (mImageviews.length % 4 != 0 && nexPosition == mImageviews.length - mImageviews.length % 4) {   //数组长度不为4的倍数
            nexPosition = mImageviews.length;
            k = mImageviews.length % 4;
            Log.e("answer","进入不为4的倍数的数组判断逻辑代码中，nextpositon:  " +nexPosition  + "    mImageviews.length % 4 :  " +mImageviews.length % 4);
        } else {
            nexPosition = nexPosition + 4;
            k = 4;   //for循环的次数为4次
        }


        Log.e("answer","nextpositon----------next------:  " +nexPosition );

        for (int i1 = 0; i1 < k; i1++) {
            //位于一行中的哪个位置
            int which_position ;
            //数组中的图片位置
            int img_position;

            //如果数组长度为4的整数就把nexPosition减4再一个一个加1
            if (mImageviews.length % 4 != 0 && nexPosition == mImageviews.length) {
                img_position = nexPosition - mImageviews.length % 4 + i1;
            } else {
                img_position = nexPosition - 4 + i1;
            }
            which_position = (img_position) % 4;

            Log.e("answer","which_position:   " + which_position + "     img_position:  " +img_position );
            switch (which_position) {
                case 0:
                    myViewHolder.imageView1.setImageResource(mImageviews[img_position]);
                    break;
                case 1:
                    myViewHolder.imageView2.setImageResource(mImageviews[img_position]);
                    break;
                case 2:
                    myViewHolder.imageView3.setImageResource(mImageviews[img_position]);
                    break;
                case 3:
                    myViewHolder.imageView4.setImageResource(mImageviews[img_position]);
                    break;
            }
        }


    }


    @Override
    public int getItemCount() {
        return mImageviews.length % 4 == 0 ? mImageviews.length / 4 : mImageviews.length / 4 + 1;
    }

    //这个继承RecyclerView.ViewHolder的类千万记得要写
    private class MyViewHolder extends RecyclerView.ViewHolder{

        public ImageView imageView1;
        private ImageView imageView2;
        private ImageView imageView3;
        private ImageView imageView4;

        private MyViewHolder(View itemView) {
            super(itemView);
            imageView1 = (ImageView) itemView.findViewById(R.id.left_1);
            imageView2 = (ImageView) itemView.findViewById(R.id.left_2);
            imageView3 = (ImageView) itemView.findViewById(R.id.left_3);
            imageView4 = (ImageView) itemView.findViewById(R.id.left_4);
        }
    }
}
